package com.example.txavdemo

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import com.pince.lifcycleliveav.em.PushType
import com.pince.lifcycleliveav.em.VIDEO_RESOLUTION_TYPE_1080_1920
import com.pince.lifcycleliveav.em.VIDEO_RESOLUTION_TYPE_320_480
import com.pince.lifcycleliveav.em.VideoQualityType
import com.pince.lifcycleliveav.pushplayer.IPushPlayer
import com.pince.lifcycleliveav.pushplayer.IPushPlayerController
import com.pince.lifcycleliveav.pushplayer.LifecycleTXPusher

class PusherActivity : Activity() {

    companion object {
        fun startPusherActivity(context: Context) {
            context.startActivity(Intent(context, PusherActivity::class.java))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pusher)
        (findViewById<View>(R.id.pusher) as LifecycleTXPusher).bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
        findViewById<View>(R.id.btn).setOnClickListener {
            val ret = (findViewById<View>(R.id.pusher) as LifecycleTXPusher).startPush("填写你的推流地址")
            if (ret == -5) {
                Log.d("TAG", String.format("---------------->%s", " startRTMPPush: license 校验失败"))
            }
            Log.d("TAG", String.format("---------------->%s", ret))
        }
        findViewById<View>(R.id.btnPreView).setOnClickListener {
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).setMirror(true)
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).mVideoEncodeGop = 1
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).startPreView(true)
        }
        //标清
        findViewById<View>(R.id.btnSD).setOnClickListener {
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).setVideoQuality(VideoQualityType.SD)
        }

        //高清
        findViewById<View>(R.id.btnHD).setOnClickListener {
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).setVideoQuality(VideoQualityType.HD)
        }

        //超清
        findViewById<View>(R.id.btnFHD).setOnClickListener {
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).setVideoQuality(VideoQualityType.FHD)
        }

        //超清
        findViewById<View>(R.id.btnBD).setOnClickListener {
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).setVideoQuality(VideoQualityType.BD)
        }

        findViewById<View>(R.id.btnVideoResolution3).setOnClickListener {
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).mVideoResolution = VIDEO_RESOLUTION_TYPE_1080_1920
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).setLivePushConfig(PushType.NONM)
        }


        findViewById<View>(R.id.btnVideoResolution1).setOnClickListener {
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).setLivePushConfig(PushType.PK)
        }

        findViewById<View>(R.id.btnVideoResolution2).setOnClickListener {
            (findViewById<View>(R.id.pusher) as LifecycleTXPusher).setLivePushConfig(PushType.NONM, VIDEO_RESOLUTION_TYPE_320_480)
        }


        /**
         * 设置播放器监听
         */
        (findViewById<View>(R.id.pusher) as LifecycleTXPusher).addPushPlayerCtroller(object : IPushPlayerController {
            override fun attachPlayer(livePlayer: IPushPlayer) {
                Log.d("TAG", String.format("---------------->%s", livePlayer.isStartPushing()))
            }

            override fun onNetStatus(bundle: Bundle) {
                Log.d("TAG", String.format("---------------->%s", bundle.toString()))
                (findViewById<View>(R.id.log) as TextView).text = bundle.toString()
            }

            override fun onPlayEvent(event: Int, param: Bundle?) {
                Log.d("TAG", String.format("---------------->%s", event))
                Log.d("TAG", String.format("---------------->%s", param.toString()))
            }
        })
    }

    override fun onResume() {
        super.onResume()
        (findViewById<View>(R.id.pusher) as LifecycleTXPusher).onActivityResume()
    }

    override fun onPause() {
        super.onPause()
        (findViewById<View>(R.id.pusher) as LifecycleTXPusher).onActivityPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        (findViewById<View>(R.id.pusher) as LifecycleTXPusher).onDestroy()
    }
}